package tgc.edu.tms.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.Permission;
import tgc.edu.tms.entity.TbMenu;
import tgc.edu.tms.service.PermissionService;
import tgc.edu.tms.service.SysRoleService;
import tgc.edu.tms.service.TbMenuService;
import tgc.edu.tms.web.form.MenuForm;
@Controller
@RequestMapping(value="/menu")
public class MenuController extends CommonController<TbMenu, Integer, MenuForm>{
	@Autowired
	private TbMenuService service;
	@Autowired
	private SysRoleService roleService;
	@Autowired
	private PermissionService permissionService;
	@Override
	public void edit(MenuForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		TbMenu model=new TbMenu();
		Integer id = form.getId();
		Integer parentId = form.getParentId();
		if(parentId!=null) {
			model.setParent(new TbMenu(parentId));
		}
		if(id!=null) {
			model=service.findById(id);
		}
		List<Permission> roles=permissionService.findAll();
		map.put("model", model);
		map.put("roles", roles);
	}
	@Override
	public Object save(MenuForm form) {
		try {
			
			TbMenu model=new TbMenu();
			Integer id = form.getId();
			if(id!=null) {
				model=service.findById(id);
			}
			Integer parentId = form.getParentId();
			if(parentId==null) {
				model.setParent(null);
			}else {
				model.setParent(new TbMenu(parentId));
			}
			Integer roleId = form.getRole().getId();
			if(roleId==null) {
				model.setRole(null);
			}else {
				//SysRole role=roleService.findById(roleId);
				Permission role = permissionService.findById(roleId);
				model.setRole(role);
			}
			BeanUtils.copyProperties(form, model,"id","role","parent");
			service.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}
	
	@RequestMapping(value="/combotree")
	@ResponseBody
	public Object combotree(MenuForm form) {
		Sort sort=Sort.by(Direction.ASC, "idx1");
		List<TbMenu> list=service.findAll(buildSpec1(), sort);
		return buildAllTree(list,form.getId());
	}
	
	
	public Specification<TbMenu> buildSpec1() {
		HashSet<Rule> rules=new HashSet<>();
			rules.add(Rule.property("parent").isNull());
		return Rule.and(rules).toSpecification();
	}
	
	public List<HashMap<String, Object>> buildAllTree(List<TbMenu> list, Integer id) {
		List<HashMap<String, Object>> result=new ArrayList<>();
		for(TbMenu dept:list) {
			if(dept.getId()!=id) {
				HashMap<String, Object> node=new HashMap<>();
				node.put("id", dept.getId()==null?"":dept.getId());
				node.put("text", dept.getName()==null?"":dept.getName());
				node.put("pid", dept.getParentId());
				node.put("nodes", buildAllTree(dept.getChildren(), id));
				result.add(node);
			}
		}
		return result;
	}
	@Override
	public Specification<TbMenu> buildSpec(MenuForm form) {
		HashSet<Rule> rules=new HashSet<>();
		String search = form.getSearch();
		if("管理员".equals(search)) {
			rules.add(Rule.property("type").equal(0));
		}else if("普通用户".equals(search)) {
			rules.add(Rule.property("type").equal(1));
		}
		return Rule.and(rules).toSpecification();
	}
	

	
}
